package com.axend.aerosense.common.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.utils.DisplayUtils;
import com.axend.aerosense.common.R;
import com.axend.aerosense.common.databinding.CommonCustomInputDialogBinding;


public class CustomInputDialog extends Dialog {
    private String title;
    private String hint;
    private int maxTextSize = 6;
    private CommonCustomInputDialogBinding databinding;

    public CustomInputDialog(Context context, String title) {
        super(context, R.style.Common_CustomDialog);
        databinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_custom_input_dialog, null, false);
        this.title = title;
        this.hint = "";
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //设置提示框位置
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
//        lp.x = DisplayUtils.dip2px(53);
//        lp.y = DisplayUtils.dip2px(186);
        lp.width = DisplayUtils.dip2px(270);
        window.setAttributes(lp);
    }

    public CustomInputDialog(Context context, String title, String titleHint) {
        super(context, R.style.Common_CustomDialog);
        databinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.common_custom_input_dialog, null, false);
        this.title = title;
        this.hint = titleHint;
        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        //设置提示框位置
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.CENTER;
//        lp.x = DisplayUtils.dip2px(53);
//        lp.y = DisplayUtils.dip2px(186);
        lp.width = DisplayUtils.dip2px(270);
        window.setAttributes(lp);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(databinding.getRoot());
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        showKeyboard(databinding.commonInputEdit);
    }

    public void setMaxTextSize(int lenth){
        if(lenth < 1){
            return;
        }
        maxTextSize = lenth;
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        databinding.commonSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(TextUtils.isEmpty(getInputValue())){
                    ToastUtils.showShort(R.string.common_params_tips0);
                    return;
                }
//                if(getInputValue().length() > maxTextSize){
//                    ToastUtils.showShort(String.format(Utils.getApp().getString(R.string.common_params_tips_param), ""+maxTextSize));
//                    return;
//                }
                if (onClickBottomListener != null) {
                    onClickBottomListener.onPositiveClick();
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        databinding.commonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickBottomListener != null) {
                    onClickBottomListener.onNegtiveClick();
                }
            }
        });
    }


    /**
     * 初始化界面控件
     */
    private void initView() {
        databinding.commonInputEdit.setHint(hint);
        databinding.commonHeadTitle.setText(title);
    }

    /**
     * 获取输入属性值
     * @return
     */
    public String getInputValue(){
        String value = null;
        try{
            value = databinding.commonInputEdit.getText().toString().trim();
        }catch (Exception e){

        }
        return value;
    }

    /**
     * 设置确定取消按钮的回调
     */
    public OnClickBottomListener onClickBottomListener;

    public CustomInputDialog setOnClickBottomListener(OnClickBottomListener onClickBottomListener) {
        this.onClickBottomListener = onClickBottomListener;
        initEvent();
        return this;
    }

    public interface OnClickBottomListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick();

        /**
         * 点击取消按钮事件
         */
        public void onNegtiveClick();
    }

    private void showKeyboard(EditText edit) {
        edit.setFocusable(true);
        edit.setFocusableInTouchMode(true);
        edit.requestFocus();
        InputMethodManager imm = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
//        imm.showSoftInputFromInputMethod(edit.getWindowToken(), 0);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }


    private void hideKeyboard(EditText edit) {
        edit.setFocusable(false);
        edit.setFocusableInTouchMode(false);
        InputMethodManager imm = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
    }

}
